﻿using Autofac;
using Autofac.Integration.Mvc;
using Common;
using Domain;
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebApp
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);          
            SetAuofacResolver();//为MVC依赖注入  
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("Web.config")));//网站首页加载此配置 
        }

        //#region 为MVC依赖注入
        ///// <summary>
        ///// 为MVC依赖注入
        ///// </summary>
        //private void SetAuofacResolver()
        //{

        //    var builder = new ContainerBuilder();           
        //    // 注册控制器
        //    builder.RegisterControllers(typeof(MvcApplication).Assembly);
        //    string repositoryLib = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["RepositoryLib"]);
        //    var repositoryAsm = Assembly.Load(File.ReadAllBytes(repositoryLib));

        //    // 注册实现IRepositoryContext的类型
        //    builder
        //            .RegisterAssemblyTypes(repositoryAsm)
        //            .Where(item => typeof(IRepositoryContext).IsAssignableFrom(item))
        //            .AsImplementedInterfaces()
        //            .InstancePerLifetimeScope();


        //    // 注册所有实现IRepository的类型
        //    builder.RegisterAssemblyTypes(repositoryAsm)
        //        .Where(type => type.GetInterfaces().ToList().Exists(item => item.IsGenericType && item.GetGenericTypeDefinition() == typeof(IRepository<>)))
        //        .AsImplementedInterfaces()
        //        .InstancePerLifetimeScope();

        //    DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));


        //}
        //#endregion

        #region 为MVC依赖注入
        /// <summary>
        /// 为MVC依赖注入
        /// </summary>
        private void SetAuofacResolver()
        {
            var builder = new ContainerBuilder();

            // 注册控制器
            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            string repositoryLib = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["RepositoryLib"]);
            var repositoryAsm = Assembly.Load(File.ReadAllBytes(repositoryLib));

            // 注册实现IRepositoryContext的类型
            builder
                .RegisterAssemblyTypes(repositoryAsm)
                .Where(item => typeof(IRepositoryContext).IsAssignableFrom(item))
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();


            // 注册所有实现IRepository的类型
            builder.RegisterAssemblyTypes(repositoryAsm)
                .Where(type => type.GetInterfaces().ToList().Exists(item => item.IsGenericType && item.GetGenericTypeDefinition() == typeof(IRepository<>)))
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
        }
        #endregion
    }
}
